package org.jeecg.modules.simulator.factory.train.load;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: gp
 * @description:
 * @author: lydms
 * @create: 2024-08-07 14:48
 **/
public class LineTrainLoader  {

    private Map<String, List<String>> lineTrainMap=new HashMap<>();
    public Map<String, List<String>> getLineTrainMap() {
        return lineTrainMap;
    }

    public void loadData() {
        readFromJson("D:/aaaa/linetrain.json");
    }

    private void readFromJson(String path){
        //TODO
        List<LineTrainItem> lineTrainList = new ArrayList<>();
        try {
            String jsonString = new String(Files.readAllBytes(Paths.get(path)));
            JSONArray jsonArray = JSON.parseArray(jsonString);
            for (int i = 0; i < jsonArray.size(); i++) {
                LineTrainItem lineTrainItem = new LineTrainItem();
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String trainId = jsonObject.getString("trainId");
                String trainNum = jsonObject.getString("trainNum");
                String linenum = jsonObject.getString("linenum");
                lineTrainItem.setTrainId(trainId);
                lineTrainItem.setTrainNum(trainNum);
                lineTrainItem.setLinenum(linenum);
                lineTrainList.add(lineTrainItem);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        Map<String, List<LineTrainItem>> groupedByTrainType = lineTrainList.stream()
                .collect(Collectors.groupingBy(LineTrainItem::getLinenum));
        for (Map.Entry<String, List<LineTrainItem>> entry : groupedByTrainType.entrySet()) {
            List<String> trainIdList = entry.getValue().stream()
                    .map(item -> item.getTrainId() + "-" + item.getTrainNum())
                    .collect(Collectors.toList());
            lineTrainMap.put(entry.getKey(), trainIdList);

        }


    }
}
@Data
class LineTrainItem{
    private String trainId;
    private String trainNum;
    private String linenum;
}